
package ar.republicagroup.gestion.view;

import ar.republicagroup.gestion.entities.Banco;
import ar.republicagroup.gestion.entities.CuentaBanco;
import ar.republicagroup.gestion.entities.CuentaTipo;
import ar.republicagroup.gestion.services.CuentaBancoServices;
import ar.republicagroup.gestion.util.SpringUtils;
import ar.republicagroup.gestion.util.TableColumnAdjuster;
import ar.republicagroup.gestion.util.messages.UIMessages;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;

public class frmCuentaBanco extends javax.swing.JFrame {
    
    CuentaBancoServices cuentaBancoServices = (CuentaBancoServices)SpringUtils.getBean("cuentaBancoServices");
    private static Logger log = Logger.getLogger(frmCuentaBanco.class);
    

    public frmCuentaBanco() {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/moneybox.png")).getImage());
        cargarDatos();
    }

    private void cargarDatos(){
        List<CuentaBanco> listCuentaBanco = cuentaBancoServices.findEntities();
        cargarTablaReportes(listCuentaBanco);
    
    }
    
    
    public static void cargarTablaReportes(List<CuentaBanco> listado){
        
        DefaultTableModel model = (DefaultTableModel) tblReporte.getModel();
        model.getDataVector().clear();
        for(CuentaBanco cuenta : listado){
            model.addRow(new Object[]{cuenta.getId(),cuenta.getNombre(),cuenta.getNumero(),cuenta.getCbu(),cuenta.getCuentaTipoId(),cuenta.getBancoId() });
        }
        
        TableColumnAdjuster tca = new TableColumnAdjuster(tblReporte);
        tca.adjustColumns();
        tblReporte.setAutoCreateRowSorter(true);
        tblReporte.setModel(model);

    }
   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelContenedor = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblReporte = new javax.swing.JTable();
        btnNuevacuenta = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("...: Gestión de cuentas bancarias :...");
        setResizable(false);

        tblReporte.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Cuenta", "Nro.Cuenta", "CBU", "Tipo cuenta", "Banco"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblReporte);

        btnNuevacuenta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/add.png"))); // NOI18N
        btnNuevacuenta.setText("Nueva cuenta");
        btnNuevacuenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevacuentaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelContenedorLayout = new javax.swing.GroupLayout(panelContenedor);
        panelContenedor.setLayout(panelContenedorLayout);
        panelContenedorLayout.setHorizontalGroup(
            panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelContenedorLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 780, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNuevacuenta))
                .addContainerGap(72, Short.MAX_VALUE))
        );
        panelContenedorLayout.setVerticalGroup(
            panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelContenedorLayout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(btnNuevacuenta)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Seleccione una cuenta"));

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/edit.png"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/delete.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnModificar)
                .addGap(26, 26, 26)
                .addComponent(btnEliminar)
                .addContainerGap(27, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnModificar)
                    .addComponent(btnEliminar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelContenedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevacuentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevacuentaActionPerformed
       modalPanelAddCuentaBanco modalPanelAdd = new modalPanelAddCuentaBanco(this, true);
       modalPanelAdd.setLocationRelativeTo(this);
       modalPanelAdd.setVisible(true);
       
    }//GEN-LAST:event_btnNuevacuentaActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed

        if(tblReporte.getSelectedRow() != -1){
        
        DefaultTableModel model = (DefaultTableModel)tblReporte.getModel();
        
        /* OBTENEMOS LOS PARAMS PARA ENVIAR AL PANEL DE EDIT*/
        Object id_cuenta            =  model.getValueAt(tblReporte.convertRowIndexToModel(tblReporte.getSelectedRow()),0);
        CuentaTipo cuenta_tipo      = (CuentaTipo)model.getValueAt(tblReporte.convertRowIndexToModel(tblReporte.getSelectedRow()), 4);
        Banco banco                 = (Banco)model.getValueAt(tblReporte.convertRowIndexToModel(tblReporte.getSelectedRow()), 5);
        
     
        modalPanelEditCuentaBanco modalPanelEdit = new modalPanelEditCuentaBanco(this, true);
        modalPanelEdit.setId_cuenta(Integer.valueOf(id_cuenta.toString()));
        modalPanelEdit.setId_banco(banco.getId());
        modalPanelEdit.setId_cuentatipo(cuenta_tipo.getId());
        modalPanelEdit.setLocationRelativeTo(this);
        modalPanelEdit.setVisible(true);
        
        } else {
            JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredSelectedCuentaBanco"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
        }
        
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if(tblReporte.getSelectedRow() != -1){
                    DefaultTableModel model = (DefaultTableModel)tblReporte.getModel();
            /* OBTENEMOS LOS PARAMS PARA ENVIAR AL PANEL DE DELETE*/
                    Object id_cuenta            =  model.getValueAt(tblReporte.getSelectedRow(),0);
                    Object nombre_cuenta        =  model.getValueAt(tblReporte.getSelectedRow(), 1);
            
                    int respuesta = JOptionPane.showConfirmDialog(this, "Desea eliminar la cuenta "+nombre_cuenta+" ?", "Eliminar cuenta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
         
                if(respuesta == 0){
                    String mensaje =  cuentaBancoServices.delete(Integer.valueOf(id_cuenta.toString()));
                    JOptionPane.showMessageDialog(this, mensaje, "Cuenta eliminada", JOptionPane.INFORMATION_MESSAGE);
                }
                cargarTablaReportes(cuentaBancoServices.findEntities());
        }else {
            JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredSelectedCuentaBanco"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
        }        
    }//GEN-LAST:event_btnEliminarActionPerformed

   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmCuentaBanco().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevacuenta;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelContenedor;
    protected static javax.swing.JTable tblReporte;
    // End of variables declaration//GEN-END:variables
}
